package crm8000.Controllers

import crm8000.Models.User;
import crm8000.services.UserService
import grails.converters.JSON

class UserController {

    static allowedMethods = [loginPOST: 'POST', changeMyPasswordPOST: 'POST']

	def UserService userService

	def login() {
		if (session.loginUserId) {
			redirect(uri: "/")
		}
	}
	
	def loginPOST() {
		def result = userService.doLogin(params.loginName, params.password)
		
		render result as JSON
	}
	
	def changeMyPassword() {
		def user = User.findByUserId(userService.loginUserId)
		
		[loginUserId: user.userId, loginUserName: user.name, loginName: user.loginName]
	}
	
	def changeMyPasswordPOST() {
		def result = userService.changeMyPassword(params.userId, params.oldPassword, params.newPassword)
		
		render result as JSON
	}
	
	def index() {
		[loginUserName: userService.loginUserName]
	}
}
